/**
 * 
 */
package edu.ou.cs.youming.agents;

import java.util.Map;
import java.util.Map.Entry;

import edu.ou.cs.youming.jaxb.types.Dictionary;
import edu.ou.cs.youming.jaxb.types.Feature;
import edu.ou.cs.youming.jaxb.types.Label;

/**
 * @author Youming Lin
 * 
 */
public final class HeuristicAgent implements Agent {
	private Dictionary dict;

	@Override
	public final void train(final Map<Feature, Label> features, final Dictionary dict) {
		if (dict == null) {
			throw new NullPointerException("dictionary");
		}

		this.dict = dict;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.ou.cs.youming.agents.Agent#predict(edu.ou.cs.youming.jaxb.types.Feature)
	 */
	@Override
	public final Label predict(final Feature feature) {
		double sentimentValue = 0d;

		for (final Entry<String, Integer> e : feature.frequencies.entrySet()) {
			try {
				sentimentValue += e.getValue() * dict.getValue(e.getKey());
			}
			catch (final IllegalArgumentException iae) {
				// word is not in dictionary; do nothing
			}
		}

		if (sentimentValue >= 0d) // predict positive
		{
			return Label.POSITIVE;
		}

		// predict negative
		return Label.NEGATIVE;
	}
}